<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-1-11
 * Time: 下午4:23
 */

namespace api\modules\loan\controllers;


use common\components\tools\Tools;
use common\models\LoanApply;
use common\models\LoanApplyImagesUpload;
use wodrow\yii\rest\Controller;
use yii\base\ErrorException;

class BusinessController extends Controller
{
    /**
     * 借款企业申请借款
     * @desc post/get
     * @param string $companyCode 公司识别码
     * @param string $requestNo 请求码
     * @param string $orderId 订单编号，如浙B04-160111-004
     * @param string $debtName 借款人姓名
     * @param string $idCardNo 借款人身份证号码
     * @param string $mobileNo 借款人手机号码
     * @param string $contract 借款合同号
     * @param serialize $other_data [$payAmount 购车总价 $downPayment 首付款 $requestTime 请求时间]
     */
    public function actionApplyLoan($companyCode, $requestNo = null, $orderId, $debtName, $idCardNo, $mobileNo, $contract, $other_data = null)
    {
        /*$x = [
            'bankCardNo' => '6228480402564890018',
            'payAmount' => 1000000,
            'downPayment' => 800000,
            'debtAmount' => 200000,
            'contract' => 'test',
        ];
        echo serialize($x);exit;*/
        if (LoanApply::findOne(['order_id'=>$orderId])){
            throw new ErrorException("已经存在借款申请订单号[{$orderId}]");
        }
        $plat_form = LoanApply::getPlatForm($companyCode);
        $plat_form->loan_apply = new LoanApply();
        if (!$requestNo){
            $requestNo = \Yii::$app->security->generateRandomString(32)."_".date("YmdHis");
        }
        $plat_form->loan_apply->request_id = $requestNo;
        $plat_form->loan_apply->order_id = $orderId;
        $plat_form->loan_apply->debt_name = $debtName;
        $plat_form->loan_apply->id_card_no = $idCardNo;
        $plat_form->loan_apply->mobile_no = $mobileNo;
        $plat_form->loan_apply->contract = $contract;
        $plat_form->loan_apply->other_data = $other_data;
        $r = $plat_form->applyLoan();
        return $r;
    }

    /**
     * 融易行借款返回接口
     * @desc get/post
     * @param string $requestNo
     * @param string $sumAmount
     * @param string(json格式化的字符串) $detail 其他订单明细信息
     * @return string requestNo
     * @return string sunAmount
     * @return complex detail
     * @return complex loan_apply 借款申请详细信息
     */
    public function actionRongYiXingBack($requestNo, $sumAmount, $detail)
    {
        $detail = json_decode($detail);
        $b = [
            'requestNo' => $requestNo,
            'sunAmount' => $sumAmount,
            'detail' => $detail,
        ];
        $loan_apply = LoanApply::findOne(['request_id'=>$requestNo]);
        if (!$loan_apply){
            throw new ErrorException('没有找到该[requestNo]请求数据,有可能是申请过程中验签失败');
        }
        $other_data = $loan_apply->other_data;
        $other_data = unserialize($other_data);
        $other_data['sunAmount'] = $sumAmount;
        $other_data['detail'] = $detail;
        $loan_apply->other_data = $other_data;
        $b['loan_apply'] = $loan_apply->toArray();
        $loan_apply->other_data = serialize($loan_apply->other_data);
        if (!$loan_apply->save()){
            throw new ErrorException("融易行借款返回数据保存失败");
        }
        return $b;
    }

    /**
     * 借款企业申请借款图片上传 [融易行] [已废弃]
     * @desc get/post
     * @param string $companyCode 借款公司编码
     * @param string $loanDate 借款日期 yyyymmdd
     * @param string $loanCode 借款编号
     * @param string $images ['type' => ['img1', 'img2'], 'type' => ['img1', 'img2']]
     * @throws
     * @return
     */
    public function actionUploadImagesRyx($companyCode, $loanDate, $loanCode, $images)
    {
        $plat_form = LoanApply::getPlatForm($companyCode);
        $bool = 1;
        $images = unserialize($images);
        $sftp_ryx = \Yii::$app->sftp_ryx;
        $dir_m = substr($loanDate, 0, 6);
        $dir_d = substr($loanDate, 6, 2);
        $dir = $dir_m.DIRECTORY_SEPARATOR.$dir_d.DIRECTORY_SEPARATOR.$loanCode;
        if ($sftp_ryx->has($dir)){
            $bool *= $sftp_ryx->deleteDir($dir);
        }
        $bool *= $sftp_ryx->createDir($dir);
        foreach ($images as $k => $v) {
            foreach ($v as $k1 => $v1){
                $bool *= $sftp_ryx->put($dir.DIRECTORY_SEPARATOR.$k.$k1.".".Tools::getExt($v1), file_get_contents($v1));
            }
        }
        return $bool;
//        $o = $sftp_ryx->listContents('', true);
//        $o['bool'] = $bool;
//        return $o;
    }

    /**
     * 借款企业申请借款图片上传 [计划任务]
     * @desc get/post
     * @param string $companyCode 借款公司编码
     * @param string $loanDate 借款日期 yyyymmdd
     * @param string $loanCode 借款编号
     * @param string $images ['type' => ['img1', 'img2'], 'type' => ['img1', 'img2']]
     * @throws
     * @return
     */
    public function actionUploadImages($companyCode, $loanDate, $loanCode, $images)
    {
        $plat_form = LoanApply::getPlatForm($companyCode);
        $m = LoanApplyImagesUpload::findOne(['company_code'=>$companyCode, 'loan_date'=>$loanDate, 'loan_code' => $loanCode]);
        if ($m){
            if ($images == $m->images){}else{
                $m->images = $images;
                $m->status = LoanApplyImagesUpload::STATUS_NO_UPLOAD;
                if ($m->save()){}else{
                    throw new ErrorException("保存异常");
                }
            }
        }else{
            $m = new LoanApplyImagesUpload();
            $m->company_code = $companyCode;
            $m->loan_date = $loanDate;
            $m->loan_code = $loanCode;
            $m->images = $images;
            $m->status = LoanApplyImagesUpload::STATUS_NO_UPLOAD;
            if ($m->save()){}else{
                throw new ErrorException("保存异常");
            }
        }
        return 1;
    }
}